<?php

namespace Stolium\TestBundle\Controller;

use Stolium\TestBundle\Entity\Level;
use Stolium\TestBundle\Form\Type\LevelType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Stolium\TestBundle\Entity\Club;
use Stolium\TestBundle\Form\Type\ClubType;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    public function indexAction($name)
    {
        return $this->render('TestBundle:Default:index.html.twig', array('name' => $name));
    }

    public function newClubAction(Request $request){
        $club = new Club();
        $form = $this->createForm(new ClubType(), $club);

        $form->handleRequest($request);

        if ($form->isValid()) {
            // perform some action, such as saving the task to the database

            $em = $this->getDoctrine()->getManager();
            $em->persist($club);
            $em->flush();

//            return $this->redirect($this->generateUrl('task_success'));
            return $this->render('TestBundle:Default:success.html.twig');
        }

        return $this->render('TestBundle:Default:newClub.html.twig', array(
            'form' => $form->createView(),
        ));
    }

    public function newLevelAction(Request $request){
        $level = new Level();
        $form = $this->createForm(new LevelType(), $level);

        $form->handleRequest($request);

        if ($form->isValid()) {

            $em = $this->getDoctrine()->getManager();
            $em->persist($level);
            $em->flush();

            return $this->render('TestBundle:Default:success.html.twig');
        }

        return $this->render('TestBundle:Default:newLevel.html.twig', array(
            'form' => $form->createView(),
        ));
    }
}
